<template>
  <div>
    <div>
      <!--Input 输入框-->
      <el-input
              class="addPosInput"
              size="medium"
              placeholder="添加职位"
              suffix-icon="el-icon-plus"
              @keydown.enter.native="addPosition"
              v-model="pos.name">
      </el-input>
      <!--Button 按钮-->
      <el-button type="primary"
                 icon="el-icon-plus"
                 size="medium"
                 @click="addPosition">添加</el-button>
    </div>
    <div class="posManaTable">
      <!--Table 表格-->
      <el-table
              :data="positions"
              stripe
              border=""
              @selection-change="handleSelectionChange"
              style="width: 100%">
        <!--多选框-->
        <el-table-column
                type="selection"
                width="55">
        </el-table-column>
        <el-table-column
                prop="id"
                label="编号"
                width="70">
        </el-table-column>
        <el-table-column
                prop="name"
                label="职位"
                width="150">
        </el-table-column>
        <el-table-column
                prop="createDate"
                label="创建时间"
                width="150">
        </el-table-column>
        <el-table-column
                prop="enabled"
                label="是否启用"
                width="100">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
            <el-tag v-else type="danger">未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
                    size="mini"
                    @click="showEditView(scope.$index, scope.row)">编辑</el-button>
            <el-button
                    size="mini"
                    type="danger"
                    @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!--批量删除按钮-->
    <el-button size="medium" type="danger" style="margin-top:8px;"
               :disabled="this.multipleSelection.length === 0"
               @click="deleteMany">批量删除</el-button>
    <!--Dialog 对话框-->
    <el-dialog
            title="编辑职位"
            :visible.sync="dialogVisible"
            width="30%">
      <div>
        <!--Tag 标签-->
        <table>
          <tr>
            <td>
              <el-tag>职位名称</el-tag>
            </td>
            <td>
              <el-input size="small" v-model="updatePos.name" class="updatePosInput"></el-input>
            </td>
          </tr>
          <tr>
            <td>
              <el-tag>是否启用</el-tag>
            </td>
            <td>
              <!--Switch 开关-->
              <el-switch
                      v-model="updatePos.enabled"
                      active-text="已启用"
                      inactive-text="未启用">
              </el-switch>
            </td>
          </tr>
        </table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取 消</el-button>
        <el-button size="small" type="primary" @click="doUpdate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "PosMana",
    data() {
      return {
        pos: {
          name: ''
        },
        updatePos: {
          name: '',
          enabled: ''
        },
        positions:[],
        dialogVisible: false,
        multipleSelection: []
      }
    },
    methods: {
      //  初始化职位table
      initPositions() {
        this.getRequest('/system/basic/pos/').then(resp => {
          if (resp) {
            this.positions = resp
          }
        })
      },
      //  添加职位
      addPosition() {
        if (this.pos.name) {
          this.postRequest('/system/basic/pos/', this.pos).then(resp => {
            if (resp) {
              this.initPositions()
              //  清空输入框内容
              this.pos.name = ''
            }
          })
        } else {
          this.$message.error("添加失败！职位名称不能为空")
        }
      },
      //  弹出职位编辑框
      showEditView(index, data) {
        //  this.updatePos = data 这个直接赋值会导致bug，输入框与table里的数据同步变化
        //  Object.assign(target, source1, source2)方法用于对象的合并，将源对象（source）的所有可枚举属性，复制到目标对象（target
        Object.assign(this.updatePos, data)
        //  createDate不设空的话后台会未知错误，这个字段后端会自己创建
        this.updatePos.createDate = ''
        this.dialogVisible = true
      },
      //  删除职位
      handleDelete(data) {
        //  MessageBox 弹框
        this.$confirm('此操作将永久删除['+ data.name +']职位, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteRequest('/system/basic/pos/' + data.id).then(resp => {
            if (resp) {
              this.initPositions()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      //  更新职位
      doUpdate() {
        this.putRequest('/system/basic/pos/', this.updatePos).then(resp => {
          if (resp) {
            this.initPositions()
            this.dialogVisible = false
          }
        })
      },
      //  获取被选中的id
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      //  批量删除
      deleteMany() {
        //  MessageBox 弹框
        this.$confirm('此操作将永久删除['+ this.multipleSelection.length +']条职位, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let ids = '?'
          this.multipleSelection.forEach(item => {
            ids += 'ids=' + item.id + '&'
          })
          this.deleteRequest('/system/basic/pos/' + ids).then(resp => {
            if (resp) {
              this.initPositions()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    },
    mounted() {
      this.initPositions()
    }
  }
</script>

<style scoped>

  .addPosInput {
    width: 300px;
  }

  .posManaTable {
    width: 100%;
    margin-top: 10px;
  }

  .updatePosInput {
    width: 200px;
    margin-left: 8px;
  }

</style>